package com.letwal.cmcvnd.earn.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.viewpager.widget.PagerAdapter
import com.letwal.cmcvnd.R
import com.letwal.cmcvnd.adata.home.getTransferInEarn.RevDataGetTransferInEarn
import com.letwal.cmcvnd.extend.toFinancePercentString
import com.letwal.cmcvnd.extend.toFinanceString
import java.math.BigDecimal
import java.math.RoundingMode

/** 结果页展示view pager 适配器 */
class EarnLevelPageAdapter(context: Context, arrayList: List<RevDataGetTransferInEarn>) :
    PagerAdapter() {
    private val mContext: Context
    private val dataList: List<RevDataGetTransferInEarn>

    private lateinit var rlEarnTransferInEarn: RelativeLayout
    private lateinit var rlEarnTransferInEarnData: RelativeLayout

    private lateinit var imageEarnLevel: ImageView
    private lateinit var textEarnLevel: TextView
    private lateinit var imageEarnGoldLevel: ImageView

    private lateinit var textEarnCurrentEarnTodayValue: TextView
    private lateinit var textEarnCurrentEarnTodayValueUnit: TextView
    private lateinit var textEarnRatioValue: TextView
    private lateinit var textEarnWithdrawalTimesValue: TextView
    private lateinit var textEarnTransferInTodayTargetValue: TextView
    private lateinit var textEarnTransferInTodayTargetValueUnit: TextView
    private lateinit var textEarnTransferInTodayValue: TextView

    private lateinit var textEarnCurrentLevelFlag: TextView
    private lateinit var progressBarTransferInTodayBronze: ProgressBar
    private lateinit var progressBarTransferInTodayGold: ProgressBar
    private lateinit var progressBarTransferInTodayPlatinum: ProgressBar
    private lateinit var progressBarTransferInTodayDiamond: ProgressBar

    init {
        mContext = context
        dataList = arrayList
    }

    companion object {
        const val LEVEL_BRONZE = "BRONZE"
        const val LEVEL_GOLD = "GOLD"
        const val LEVEL_PLATINUM = "PLATINUM"
        const val LEVEL_DIAMOND = "DIAMOND"
    }

    /**
     * 点击回调
     *
     * @return Int 位置
     * @return RevObjAiHomeDemo 值
     * */
    private var mItemClickListener: (Int, RevDataGetTransferInEarn) -> Unit =
        { position, obj -> }

    @SuppressLint("SetTextI18n")
    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        //子View显示
        val view: View =
            View.inflate(mContext, R.layout.view_adpter_earn_level, null)

        rlEarnTransferInEarn = view.findViewById(R.id.rlEarnTransferInEarn)
        rlEarnTransferInEarnData = view.findViewById(R.id.rlEarnTransferInEarnData)
        imageEarnLevel = view.findViewById(R.id.imageEarnLevel)
        textEarnLevel = view.findViewById(R.id.textEarnLevel)
        imageEarnGoldLevel = view.findViewById(R.id.imageEarnGoldLevel)

        textEarnCurrentEarnTodayValue = view.findViewById(R.id.textEarnCurrentEarnTodayValue)
        textEarnCurrentEarnTodayValueUnit =
            view.findViewById(R.id.textEarnCurrentEarnTodayValueUnit)
        textEarnRatioValue = view.findViewById(R.id.textEarnRatioValue)
        textEarnWithdrawalTimesValue = view.findViewById(R.id.textEarnWithdrawalTimesValue)
        textEarnTransferInTodayTargetValue =
            view.findViewById(R.id.textEarnTransferInTodayTargetValue)
        textEarnTransferInTodayTargetValueUnit =
            view.findViewById(R.id.textEarnTransferInTodayTargetValueUnit)
        textEarnTransferInTodayValue = view.findViewById(R.id.textEarnTransferInTodayValue)

        textEarnCurrentLevelFlag = view.findViewById(R.id.textEarnCurrentLevelFlag)
        progressBarTransferInTodayBronze = view.findViewById(R.id.progressBarTransferInTodayBronze)
        progressBarTransferInTodayGold = view.findViewById(R.id.progressBarTransferInTodayGold)
        progressBarTransferInTodayPlatinum =
            view.findViewById(R.id.progressBarTransferInTodayPlatinum)
        progressBarTransferInTodayDiamond =
            view.findViewById(R.id.progressBarTransferInTodayDiamond)

        val item = dataList[position]
        if (item.level?.equals(LEVEL_BRONZE, true) == true) {
            rlEarnTransferInEarn.setBackgroundResource(R.drawable.earn_level1)
            imageEarnLevel.setImageResource(R.drawable.icon_earn_level_1)
            imageEarnGoldLevel.setImageResource(R.drawable.icon_earn_level_1)
            textEarnLevel.text = LEVEL_BRONZE

            progressBarTransferInTodayBronze.visibility = View.VISIBLE
            progressBarTransferInTodayGold.visibility = View.GONE
            progressBarTransferInTodayPlatinum.visibility = View.GONE
            progressBarTransferInTodayDiamond.visibility = View.GONE

        } else if (item.level?.equals(LEVEL_GOLD, true) == true) {
            rlEarnTransferInEarn.setBackgroundResource(R.drawable.earn_level2)
            imageEarnLevel.setImageResource(R.drawable.icon_earn_level_2)
            imageEarnGoldLevel.setImageResource(R.drawable.icon_earn_level_2)
            textEarnLevel.text = LEVEL_GOLD

            progressBarTransferInTodayBronze.visibility = View.GONE
            progressBarTransferInTodayGold.visibility = View.VISIBLE
            progressBarTransferInTodayPlatinum.visibility = View.GONE
            progressBarTransferInTodayDiamond.visibility = View.GONE

        } else if (item.level?.equals(LEVEL_PLATINUM, true) == true) {
            rlEarnTransferInEarn.setBackgroundResource(R.drawable.earn_level3)
            imageEarnLevel.setImageResource(R.drawable.icon_earn_level_3)
            imageEarnGoldLevel.setImageResource(R.drawable.icon_earn_level_3)
            textEarnLevel.text = LEVEL_PLATINUM

            progressBarTransferInTodayBronze.visibility = View.GONE
            progressBarTransferInTodayGold.visibility = View.GONE
            progressBarTransferInTodayPlatinum.visibility = View.VISIBLE
            progressBarTransferInTodayDiamond.visibility = View.GONE

        } else if (item.level?.equals(LEVEL_DIAMOND, true) == true) {
            rlEarnTransferInEarn.setBackgroundResource(R.drawable.earn_level4)
            imageEarnLevel.setImageResource(R.drawable.icon_earn_level_4)
            imageEarnGoldLevel.setImageResource(R.drawable.icon_earn_level_4)
            textEarnLevel.text = LEVEL_DIAMOND

            progressBarTransferInTodayBronze.visibility = View.GONE
            progressBarTransferInTodayGold.visibility = View.GONE
            progressBarTransferInTodayPlatinum.visibility = View.GONE
            progressBarTransferInTodayDiamond.visibility = View.VISIBLE
        }

        rlEarnTransferInEarnData.visibility = View.VISIBLE
        imageEarnLevel.visibility = View.GONE

        textEarnCurrentEarnTodayValue.text = item.todayIncome.toFinanceString()
        textEarnCurrentEarnTodayValueUnit.text = "USDT"
        textEarnRatioValue.text = item.incomeRate.toFinancePercentString()
        textEarnWithdrawalTimesValue.text =
            item.remindTimes.toString() + "/" + item.totalTimes.toString()
        textEarnTransferInTodayTargetValue.text = item.nextAmount
        textEarnTransferInTodayTargetValueUnit.text = "USDT"
        textEarnTransferInTodayValue.text =
            item.todayAmount.toFinanceString()

        //计算进度
        val todayAmountBig = BigDecimal(item.todayAmount)
        val nextAmountBig = BigDecimal(item.nextAmount.replace("+", ""))
        if (nextAmountBig.toFloat() <= 0) {
            progressBarTransferInTodayBronze.progress = 5
            progressBarTransferInTodayGold.progress = 5
            progressBarTransferInTodayPlatinum.progress = 5
            progressBarTransferInTodayDiamond.progress = 5

        } else if (todayAmountBig.toFloat() <= 0) {
            progressBarTransferInTodayBronze.progress = 5
            progressBarTransferInTodayGold.progress = 5
            progressBarTransferInTodayPlatinum.progress = 5
            progressBarTransferInTodayDiamond.progress = 5

        } else {
            val divBig = todayAmountBig.divide(nextAmountBig, RoundingMode.HALF_UP)
            val perBig = divBig.multiply(BigDecimal("100"))
            progressBarTransferInTodayBronze.progress = perBig.toInt()
            progressBarTransferInTodayGold.progress = perBig.toInt()
            progressBarTransferInTodayPlatinum.progress = perBig.toInt()
            progressBarTransferInTodayDiamond.progress = perBig.toInt()
        }

        //当前等级
        if (item.isValidData) {
            textEarnCurrentLevelFlag.visibility = View.VISIBLE
        } else {
            textEarnCurrentLevelFlag.visibility = View.GONE
        }

        //点击事件
        rlEarnTransferInEarnData.setOnClickListener {
            mItemClickListener.invoke(position, item)
        }

        //添加到父控件
        container.addView(view)

        return view
    }

    override fun getCount(): Int {
        return dataList.size
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view === `object`
    }

    override fun destroyItem(container: ViewGroup, position: Int, obj: Any) {
        //从viewpager中移除掉
        container.removeView(obj as View)
    }

    override fun getItemPosition(`object`: Any): Int {
        return POSITION_NONE
    }

    /**
     * 设置点击回调
     */
    fun setItemClickListener(listener: (Int, RevDataGetTransferInEarn) -> Unit) {
        mItemClickListener = listener
    }
}